package com.ruoyi.base.controller;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import com.ruoyi.base.domain.BaseRoom;
import com.ruoyi.base.service.IBaseRoomService;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 房间Controller
 * 
 * @author yepanpan
 * @date 2020-11-09
 */
@Api("房间信息管理")
@RestController
@RequestMapping("/base/room")
public class BaseRoomController extends BaseController
{
    @Autowired
    private IBaseRoomService baseRoomService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询房间列表
     */
    @ApiOperation("获取房间列表")
    @PreAuthorize("@ss.hasPermi('base:room:list')")
    @GetMapping("/list")
    public TableDataInfo list(BaseRoom baseRoom)
    {
        startPage();
        List<BaseRoom> list = baseRoomService.selectBaseRoomList(baseRoom);
        return getDataTable(list);
    }

    /**
     * 查询房间下拉列表
     */
    @ApiOperation("获取房间列表")
    @GetMapping("/select")
    public TableDataInfo select(BaseRoom baseRoom)
    {
        List<BaseRoom> list = baseRoomService.selectList(baseRoom);
        return getDataTable(list);
    }
    
    /**
     * 查询可用教室列表
     */
    @ApiOperation("获取可用教室列表")
    @GetMapping("/clazz")
    public TableDataInfo clazz(BaseRoom baseRoom)
    {
        List<BaseRoom> list = baseRoomService.selectClassRoom(baseRoom);
        return getDataTable(list);
    }

    /**
     * 导出房间列表
     */
    @ApiOperation("导出房间列表")
    @PreAuthorize("@ss.hasPermi('base:room:export')")
    @Log(title = "房间", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BaseRoom baseRoom)
    {
        List<BaseRoom> list = baseRoomService.selectBaseRoomList(baseRoom);
        ExcelUtil<BaseRoom> util = new ExcelUtil<BaseRoom>(BaseRoom.class);
        return util.exportExcel(list, "room");
    }

    /**
     * 获取房间详细信息
     */
    @ApiOperation("获取房间详细信息")
    @ApiImplicitParam(name = "id", value = "房间ID", required = true, dataType = "int", paramType = "path")
    @PreAuthorize("@ss.hasPermi('base:room:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(baseRoomService.selectBaseRoomById(id));
    }

    /**
     * 新增房间
     */
    @ApiOperation("新增房间")
    @PreAuthorize("@ss.hasPermi('base:room:add')")
    @Log(title = "房间", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BaseRoom baseRoom)
    {
        return toAjax(baseRoomService.insertBaseRoom(baseRoom));
    }

    /**
     * 修改房间
     */
    @ApiOperation("修改房间")
    @PreAuthorize("@ss.hasPermi('base:room:edit')")
    @Log(title = "房间", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BaseRoom baseRoom)
    {
        return toAjax(baseRoomService.updateBaseRoom(baseRoom));
    }

    /**
     * 删除房间
     */
    @ApiOperation("删除房间")
    @ApiImplicitParam(name = "id", value = "房间ID", required = true, dataType = "int", paramType = "path")
    @PreAuthorize("@ss.hasPermi('base:room:remove')")
    @Log(title = "房间", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(baseRoomService.deleteBaseRoomByIds(ids));
    }
    
    @Log(title = "导入房间信息", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('base:room:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<BaseRoom> util = new ExcelUtil<BaseRoom>(BaseRoom.class);
        List<BaseRoom> roomList = util.importExcel(file.getInputStream());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = loginUser.getUsername();
        String message = baseRoomService.importRoom(roomList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<BaseRoom> util = new ExcelUtil<BaseRoom>(BaseRoom.class);
        return util.importTemplateExcel("房间数据");
    }
}
